import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',//首页
    name: 'home',
    component: HomeView,
  },
  {
    path: '/leader',//排行榜
    name: 'leader',
    component: () => import('../views/LeaderBoard.vue'),
  },
  {
    path: '/classify',//分类
    name: 'classify',
    component: () => import('../views/ClassifyView.vue'),
  },
  {
    path: '/schoolboy',//男生
    name: 'schoolboy',
    component: () => import('../views/SchoolBoy.vue'),
  },
  {
    path: '/schoolgirl',//女生
    name: 'schoolgirl',
    component: () => import('../views/SchoolGirl.vue'),
  },
  {
    path: '/switch',//切换产品
    name: 'switch',
    component: () => import('../views/switch/SwitchProducts.vue'),
  },
  {
    path: '/bookclassify',//图书分类
    name: 'bookclassify',
    component: () => import('../views/bottomnav/BookClassify.vue'),
  },
  {
    path: '/bookshelf',//切换产品
    name: 'bookshelf',
    component: () => import('../views/bookshelf/BookShelf.vue'),
  },
  {
    path: '/search',//搜索
    name: 'search',
    component: () => import('../views/searchbar/searchView.vue'),
  },
  {
    path: '/noveldetail',//小说介绍
    name: 'noveldetail',
    component: () => import('../views/DetailPage/NoveldetailPage.vue'),
  },
  {
    path: '/monthly',//包月
    name: 'monthly',
    component: () => import('../views/bottomnav/MonthlyView.vue'),
  },
  {
    path: '/gratis',//免费
    name: 'gratis',
    component: () => import('../views/bottomnav/GratisView.vue'),
  },
  {
    path: '/finish',//完结
    name: 'finish',
    component: () => import('../views/bottomnav/FinishView.vue'),
  },
  {
    path: '/newbook',//新书
    name: 'newbook',
    component: () => import('../views/bottomnav/NewbookView.vue'),
  },
  {
    path: '/detailPage',//详情页
    name: 'detailPage',
    component: () => import('../views/DetailPage/DetailPage.vue'),

  },
  {
    path: '/novelMain',//小说
    name: 'novelMain',
    component: () => import('../views/novel/novelMain.vue'),
  },
  {
    path: '/percenter',//个人中心
    name: 'percenter',
    component: () => import('../views/login/PersonalView.vue'),
  },
  {
    path: '/personal',//登陆后的页面
    name: 'personal',
    component: () => import('../views/login/PersonalCenter.vue'),

    // 独享路由守卫
    beforeEnter: (to, from, next) => {
      let token = window.localStorage.getItem("token");
      // 判断是否登录
      if (!token) {
        next("/login");
        return;
      } else {
        next();
      }
    }
  },
  {
    path: '/login',//登录
    name: 'login',
    component: () => import('../views/login/loginView.vue'),
  },
]


// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
}


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior: function () {
    return { x: 0, y: 0 }
  }
})

// router.beforeEach((to,from,next)=>{
//   let token = window.localStorage.getItem("token");
//   // 登录界面判断
//   if(to.path == "/login"){
//       next();
//       return;
//   }else{ //判断是否登录
//     if(!token){
//       next("/login");//进入登录页面
//       return;
//     }else{
//       next();//执行下一个路由
//     }
//   }
// })

export default router
